Page({
  data: {
    userInfo: {},
    checkinRecord: [],
    continuousDays: 0,
    totalPoints: 0,
    todayChecked: false,
    currentMonth: '',
    checkinCalendar: []
  },

  onLoad() {
    this.loadUserInfo();
    this.loadCheckinData();
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.loadCheckinData();
  },

  // 加载用户信息
  loadUserInfo() {
    const app = getApp();
    const userInfo = app.globalData.userInfo || {};
    this.setData({
      userInfo: userInfo
    });
  },

  // 加载签到数据
  loadCheckinData() {
    const now = new Date();
    const currentMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
    
    // Mock签到数据
    const mockCheckinRecord = [
      {
        date: '2024-01-15',
        points: 20,
        continuous: 5
      },
      {
        date: '2024-01-14',
        points: 20,
        continuous: 4
      },
      {
        date: '2024-01-13',
        points: 20,
        continuous: 3
      },
      {
        date: '2024-01-12',
        points: 20,
        continuous: 2
      },
      {
        date: '2024-01-11',
        points: 20,
        continuous: 1
      },
      {
        date: '2024-01-10',
        points: 20,
        continuous: 0
      },
      {
        date: '2024-01-09',
        points: 20,
        continuous: 0
      },
      {
        date: '2024-01-08',
        points: 20,
        continuous: 0
      },
      {
        date: '2024-01-07',
        points: 20,
        continuous: 0
      },
      {
        date: '2024-01-06',
        points: 20,
        continuous: 0
      }
    ];

    const today = now.toISOString().split('T')[0];
    const todayChecked = mockCheckinRecord.some(record => record.date === today);
    
    const continuousDays = todayChecked ? mockCheckinRecord[0].continuous : 0;
    const totalPoints = mockCheckinRecord.reduce((sum, record) => sum + record.points, 0);

    // 生成日历数据
    const checkinCalendar = this.generateCalendar(currentMonth, mockCheckinRecord);

    this.setData({
      checkinRecord: mockCheckinRecord,
      continuousDays: continuousDays,
      totalPoints: totalPoints,
      todayChecked: todayChecked,
      currentMonth: currentMonth,
      checkinCalendar: checkinCalendar
    });
  },

  // 生成日历数据
  generateCalendar(month, checkinRecord) {
    const [year, monthNum] = month.split('-').map(Number);
    const firstDay = new Date(year, monthNum - 1, 1);
    const lastDay = new Date(year, monthNum, 0);
    const startDate = new Date(firstDay);
    startDate.setDate(startDate.getDate() - firstDay.getDay());
    
    const calendar = [];
    const checkinDates = checkinRecord.map(record => record.date);
    
    for (let i = 0; i < 42; i++) {
      const currentDate = new Date(startDate);
      currentDate.setDate(startDate.getDate() + i);
      const dateStr = currentDate.toISOString().split('T')[0];
      
      calendar.push({
        date: dateStr,
        day: currentDate.getDate(),
        isCurrentMonth: currentDate.getMonth() === monthNum - 1,
        isChecked: checkinDates.includes(dateStr),
        isToday: dateStr === new Date().toISOString().split('T')[0]
      });
    }
    
    return calendar;
  },

  // 签到
  onCheckin() {
    if (this.data.todayChecked) {
      wx.showToast({
        title: '今日已签到',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '签到中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      // 模拟签到成功
      const newContinuousDays = this.data.continuousDays + 1;
      const points = 20 + (newContinuousDays >= 7 ? 10 : 0); // 连续7天额外奖励
      
      const newRecord = {
        date: new Date().toISOString().split('T')[0],
        points: points,
        continuous: newContinuousDays
      };

      const newCheckinRecord = [newRecord, ...this.data.checkinRecord];
      const newTotalPoints = this.data.totalPoints + points;

      this.setData({
        checkinRecord: newCheckinRecord,
        continuousDays: newContinuousDays,
        totalPoints: newTotalPoints,
        todayChecked: true
      });

      // 更新用户积分
      const app = getApp();
      const newUserInfo = { ...this.data.userInfo, points: this.data.userInfo.points + points };
      app.globalData.userInfo = newUserInfo;
      this.setData({ userInfo: newUserInfo });

      wx.showToast({
        title: `签到成功 +${points}积分`,
        icon: 'success'
      });
    }, 1000);
  },

  // 查看积分明细
  onViewPointsDetail() {
    wx.navigateTo({
      url: '/my/pages/points-detail/points-detail'
    });
  },

  // 分享签到
  onShareCheckin() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  }
}); 